/**
 * @Description: 文件工具
 * @author developer-z
 * @date 2021/3/20
 */

import moment from 'dayjs';
import {DatePattern} from './TimeUtils'

// 下载文件
export const stream2xlsx = blobStream => {
  const blob = new Blob([blobStream], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  });
  const fileName = `${document.title}-${moment().format(DatePattern.patternYMDHMS)}.xlsx`;
  if ('download' in document.createElement('a')) {
    // 非IE下载
    const elink = document.createElement('a');
    elink.download = fileName;
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, fileName)
  }
};